﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;

namespace WebApplication1.Account
{
    public partial class Login : System.Web.UI.Page
    {
        sv_QuanLyNhaSach.AdapterClient xuly = new sv_QuanLyNhaSach.AdapterClient();
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!IsPostBack)
            {
                initCookies();   
            }
        }

        public void initCookies()
        {
            if (Request.Browser.Cookies)
            {
                if (Request.Cookies.Get("Login") != null)
                {
                    HttpCookie MyCookies = Request.Cookies["Login"];
                    string TenDangNhap = "";
                    string MatKhau = "";
                    TenDangNhap = MyCookies["UserName"];
                    MatKhau = MyCookies["PassWord"];
                    if (TenDangNhap != "" && MatKhau != "")
                    {
                        KiemtraLogin(TenDangNhap, MatKhau);
                    }
                }
            }
        }

        protected void LoginButton_Click(object sender, EventArgs e)
        {
            if (UserName.Text != "" && Password.Text != "")
            {
                KiemtraLogin(UserName.Text, Password.Text);
            }
        }

        public void KiemtraLogin(string UserName, string Password)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(xuly.LayDS_TaiKhoanOnline()));
            if (ds.Tables.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (UserName == ds.Tables[0].Rows[i]["Tendangnhap"].ToString().Trim() && Password == ds.Tables[0].Rows[i]["Matkhau"].ToString().Trim())
                    {
                        Session.Add("UserName", UserName);
                        Session.Add("PassWord", Password);
                        Session["MaTK"] = LayMaTK(Session["UserName"].ToString());
                        //LayTT(Session["TenDangNhap"].ToString());
                        if (RememberMe.Checked == true)
                        {     
                            if (Request.Browser.Cookies)
                            {
                                if (Request.Cookies["Login"] == null)
                                {
                                    HttpCookie Cookies = new HttpCookie("Login");
                                    Cookies["UserName"] = UserName;
                                    Cookies["PassWord"] = Password;
                                    Cookies.Expires = DateTime.Now.AddDays(10);
                                    Response.Cookies.Add(Cookies);
                                }
                            }
                        }
                        Response.Redirect("../TrangChu.aspx");
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Mật khẩu hoặc tên đăng nhập không chính xác.');", true);
                    }
                }
            }

        }
        private string LayMaTK(string p)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(xuly.selectqueryByText("select matk from TaiKhoanOnline where TenDangNhap='" + p + "' ")));
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0].Rows[0][0].ToString();
            }
            return "";
        }
    }
}